﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_OwnershipHistory
    {
        public bool CreateOneOwnershipHistory(OwnershipHistory_Info objOwnershipHistory_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("OwnershipHistory_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"OwnerID", objOwnershipHistory_Info.OwnerID));
            command.Parameters.Add(new SqlParameter(@"AuctionMarketID", objOwnershipHistory_Info.AuctionMarketID));
            command.Parameters.Add(new SqlParameter(@"LivestockID", objOwnershipHistory_Info.LivestockID));
            command.Parameters.Add(new SqlParameter(@"Illnesses", objOwnershipHistory_Info.Illnesses));
            command.Parameters.Add(new SqlParameter(@"Treatment", objOwnershipHistory_Info.Treatment));
            command.Parameters.Add(new SqlParameter(@"AcquiredDate", objOwnershipHistory_Info.AcquiredDate));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneOwnershipHistory(OwnershipHistory_Info objOwnershipHistory_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("OwnershipHistory_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@OwnerID", objOwnershipHistory_Info.OwnerID));
            command.Parameters.Add(new SqlParameter("@AuctionMarketID", objOwnershipHistory_Info.AuctionMarketID));
            command.Parameters.Add(new SqlParameter("@LivestockID", objOwnershipHistory_Info.LivestockID));
            command.Parameters.Add(new SqlParameter("@Illnesses", objOwnershipHistory_Info.Illnesses));
            command.Parameters.Add(new SqlParameter("@Treatment", objOwnershipHistory_Info.Treatment));
            command.Parameters.Add(new SqlParameter("@AcquiredDate", objOwnershipHistory_Info.AcquiredDate));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneOwnershipHistory(int OwnerID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("OwnershipHistory_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"OwnerID", OwnerID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllOwnershipHistory()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("OwnershipHistory_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public OwnershipHistory_Info GetOneOwnershipHistory(int OwnerID)
        {
            OwnershipHistory_Info objOwnershipHistory_Info = new OwnershipHistory_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("OwnershipHistory_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"OwnerID", OwnerID));
            try
            {
                objConnection.Open();
                SqlDataReader ordOwnershipHistory = command.ExecuteReader(); //OjectRecordDatareader
                ordOwnershipHistory.Read();
                if (ordOwnershipHistory.HasRows)
                {
                    if (ordOwnershipHistory["OwnerID"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.OwnerID = Convert.ToInt16(ordOwnershipHistory["OwnerID"]);
                    }
                    if (ordOwnershipHistory["AuctionMarketID"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.AuctionMarketID = Convert.ToInt16(ordOwnershipHistory["AuctionMarketID"]);
                    }
                    if (ordOwnershipHistory["LivestockID"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.LivestockID = Convert.ToInt16(ordOwnershipHistory["LivestockID"]);
                    }
                    if (ordOwnershipHistory["Illnesses"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.Illnesses = Convert.ToString(ordOwnershipHistory["Illnesses"]);
                    }
                    if (ordOwnershipHistory["Treatment"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.Treatment = Convert.ToString(ordOwnershipHistory["Treatment"]);
                    }
                    if (ordOwnershipHistory["AcquiredDate"] != DBNull.Value)
                    {
                        objOwnershipHistory_Info.AcquiredDate = Convert.ToDateTime(ordOwnershipHistory["AcquiredDate"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objOwnershipHistory_Info;
        }
    }
}
